package en.edu.zxj.rabbit.extensions.demo.config;

import en.edu.zxj.rabbit.extensions.demo.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created with IntelliJ IDEA.
 * Description：
 *
 * @author: zxj
 * @date: 2024-09-20
 * @time: 19:37:47
 */
// @Configuration
public class RabbitMQConfig {


    // 声明队列
    @Bean("ackQueue")
    public Queue ackQueue() {
        return QueueBuilder.durable(Constants.ACK_QUEUE_NAME).build();
    }
    // 声明交换机
    @Bean("ackExchange")
    public Exchange ackExchange() {
        return ExchangeBuilder.directExchange(Constants.ACK_EXCHANGE_NAME).durable(true).build();
    }
    // 绑定队列和交换机
    @Bean("ackBinding")
    public Binding ackBinding(@Qualifier("ackQueue") Queue queue, @Qualifier("ackExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(Constants.ACK_ROUTING_KEY).noargs();
    }

    // 确认队列
    @Bean("confirmQueue")
    public Queue confirmQueue() {
        return QueueBuilder.durable(Constants.CONFIRM_QUEUE_NAME).build();
    }
    // 确认交换机
    @Bean("confirmExchange")
    public Exchange confirmExchange() {
        return ExchangeBuilder.directExchange(Constants.CONFIRM_EXCHANGE_NAME).durable(true).build();
    }
    // 绑定队列和交换机
    @Bean("confirmBinding")
    public Binding confirmBinding(@Qualifier("confirmQueue") Queue queue, @Qualifier("confirmExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(Constants.CONFIRM_ROUTING_KEY).noargs();
    }

    // 重试机制
    @Bean("retryQueue")
    public Queue retryQueue() {
        return QueueBuilder.durable(Constants.RETRY_QUEUE_NAME).build();
    }
    @Bean("retryExchange")
    public Exchange retryExchange() {
        return ExchangeBuilder.directExchange(Constants.RETRY_EXCHANGE_NAME).durable(true).build();
    }
    @Bean("retryBinding")
    public Binding retryBinding(@Qualifier("retryQueue") Queue queue, @Qualifier("retryExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(Constants.RETRY_ROUTING_KEY).noargs();
    }


    // ttl
    @Bean("ttlQueue")
    public Queue ttlQueue() {
        // 该队列不会过期
        return QueueBuilder.durable(Constants.TTL_QUEUE_NAME).build();
    }
    @Bean("ttlExchange")
    public FanoutExchange ttlExchange() {
        return ExchangeBuilder.fanoutExchange(Constants.TTL_EXCHANGE_NAME).durable(true).build();
    }
    @Bean("ttlBinding")
    public Binding ttlBinding(@Qualifier("ttlQueue") Queue queue, @Qualifier("ttlExchange") FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }
    @Bean("ttlQueue2")
    public Queue ttlQueue2() {
        // 设置队列的过期时间
        return QueueBuilder.durable(Constants.TTL_QUEUE_NAME2).ttl(20*1000).build();
    }

    @Bean("ttlBinding2")
    public Binding ttlBinding2(@Qualifier("ttlQueue2") Queue queue, @Qualifier("ttlExchange") FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }
}
